<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">

      <el-form-item label="年月"  >
        <MonthPicker @change="chageDate" lang="zh" :default-month="queryParams.month"></MonthPicker>
      </el-form-item>
      <el-form-item label="城市" prop="cityCode">
        <el-select v-model="queryParams.cityCode" placeholder="(输入2个字符触发搜索)" clearable filterable remote :remote-method="queryCity"  :loading="loading">
          <el-option v-for="item in citys" :key="item.code" :label="item.name  + (item.belongs ? '' : '(' + item.code + ')')" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:socialsecurity:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:socialsecurity:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:socialsecurity:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:socialsecurity:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="socialsecurityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年" align="center" prop="year" />
      <el-table-column label="城市名称" align="center" prop="cityName" />
      <el-table-column label="城市代码" align="center" prop="cityCode" />
      <el-table-column label="基数" align="center" prop="baseAmt" />
      <el-table-column label="倍数" align="center" >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0" >最低</span>
          <span v-if="scope.row.type == 1" >一倍</span>
          <span v-if="scope.row.type == 2" >两倍</span>
          <span v-if="scope.row.type == 3" >三倍</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:socialsecurity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:socialsecurity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台级每年社保基数维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="年月"  >
          <MonthPicker @change="chageDate" lang="zh" :default-month="queryParams.month"></MonthPicker>
        </el-form-item>
        <el-form-item label="城市" prop="cityCode">
          <el-select v-model="form.cityCode" placeholder="(输入2个字符触发搜索)" clearable filterable remote :remote-method="queryCity"  :loading="loading">
            <el-option v-for="item in citys" :key="item.code" :label="item.name  + (item.belongs ? '' : '(' + item.code + ')')" :value="item.code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="社保基数类型" clearable filterable remote :remote-method="queryCity"  :loading="loading">
            <el-option key="0" label="最低" value="0"></el-option>
            <el-option key="1" label="一倍" value="1"></el-option>
            <el-option key="2" label="两倍" value="2"></el-option>
            <el-option key="3" label="三倍" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="基数" prop="baseAmt">
          <el-input
              v-model="form.baseAmt"
              placeholder="请输入基数"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addSocialsecurity, delSocialsecurity, exportSocialsecurity, getSocialsecurity, listSocialsecurity, updateSocialsecurity} from "@/api/business/socialsecurity";

  import {MonthPicker} from '@/views/components/wowmonthpicker/src/index'
  import {getMonthOrDefultThisMonth} from "@/utils";
  import {listSyscityLikeName} from "@/api/business/syscity";

  export default {
  name: "Socialsecurity",
  components: {  MonthPicker },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 平台级每年社保基数维护表格数据
      socialsecurityList: [],
      citys: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        yearMonth: new Date(),
        cityCode: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
            { required: true, message: "年不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.month = getMonthOrDefultThisMonth();
    this.getList();
  },
  methods: {
    /** 查询平台级每年社保基数维护列表 */
    getList() {
      this.loading = true;
      listSocialsecurity(this.queryParams).then(response => {
        this.socialsecurityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        year: undefined,
        cityName: undefined,
        cityCode: undefined,
        baseAmt: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台级每年社保基数维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSocialsecurity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台级每年社保基数维护";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form.yearMonth = this.queryParams.yearMonth;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSocialsecurity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSocialsecurity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除平台级每年社保基数维护编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSocialsecurity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有平台级每年社保基数维护数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSocialsecurity(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    chageDate(date) {
      this.queryParams.yearMonth = date.year + '-' + date.monthIndex;
      this.queryParams.month = date.monthIndex;
    },
    queryCity(cityName) {
      if(!! cityName && cityName.length > 1) {
        listSyscityLikeName(cityName).then(response => {
          this.citys = response.rows;
        });
      }
    }

  }
};
</script>
